<?php
namespace app\admin\controller;

use think\facade\Db;
use think\Page;
use think\facade\View;
use app\common\logic\OrderLogic;
class AuthDomain extends Base {
    public function index(){
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('domain')){
                $map['domain'] = input('domain');
            }
            if(input('name')){
                $map['name'] = ['like','%'.input('name').'%'];
            }
            $count = Db::name('auth_domain')->alias('u')->where($map)->count();

            $field = '*';
            $list = Db::name('auth_domain')
                ->field($field)
                ->where($map)
                ->order('id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $list[$k] = $v;
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        return View::fetch();
    }

    /**
     * 会员详细信息查看
     */
    public function edit(){
        $id = input('id');
        $info = Db::name('auth_domain')->where('id',$id)->find();
        if (IS_POST) {
            $post = input();
            Db::startTrans();
            $data = [];
            $data['domain'] = $post['domain'];
            $data['name'] = $post['name'];
            $data['remark'] = $post['remark'];
            $data['w_time'] = time();
            if($id){
                $rs = Db::name('auth_domain')->where('id',$id)->update($data);
            }else{
                $rs = Db::name('auth_domain')->insert($data);
            }
            Db::commit();
            return json(['status'=>true,'msg'=>'操作成功']);
        }
        View::assign('info',$info);
        return View::fetch();
    }

    //删除会员
    public function del(){
        $id_arr = input('ids/a');
        //$id_arr = explode(',',$user_ids);
        Db::startTrans();
        Db::name('auth_domain')->where(['id'=>['in',$id_arr]])->delete();
        Db::commit();
        return json(['status'=>1,'msg'=>'删除成功!']);
    }

    public function changeStatus(){
        $id = input('id');
        $status = Db::name('auth_domain')->where('id',$id)->value('status');
        Db::name('auth_domain')->where('id',$id)->update(['status'=>$status?0:1]);
        return json(['status'=>1,'msg'=>'修改成功']);
    }
    public function changeBeianStatus(){
        $id = input('id');
        $status = Db::name('auth_domain')->where('id',$id)->value('beian');
        Db::name('auth_domain')->where('id',$id)->update(['beian'=>$status?0:1]);
        return json(['status'=>1,'msg'=>'修改成功']);
    }


}